import {
	defineStore
} from 'pinia';
import appApi from '/shop/api/app';
import $router from '/shop/router';
import {
	ref,
	reactive
} from 'vue';
import $platform from '/shop/platform';
import user from './user';
import sys from './sys';
import {
	baseUrl
} from '/shop/config';
const app = defineStore(
	'app',
	() => {
		let info = reactive({
			name: '', // 商城名称
			logo: '', // logo
			version: '', // 版本号
			cdnurl: '', // 云存储域名
			filesystem: '', // 云存储平台
			user_protocol: {}, // 用户协议
			privacy_protocol: {}, // 隐私协议
			about_us: {}, // 关于我们
			copyright: '', // 版权信息 I
			copytime: '', // 版权信息 II
		});

		let username = ref();

		let platform = reactive({
			payment: [], // 支持的支付方式
			recharge_payment: [], // 支持的充值支付方式
			share: {
				methods: [], // 支持的分享方式
				forwardInfo: {}, // 默认转发信息
				posterInfo: {}, // 海报信息
				linkAddress: '', // 复制链接地址
			},
			auto_login: 1, // 自动登陆
			bind_mobile: 1, // 登陆后绑定手机号提醒 (弱提醒，可手动关闭)
		});

		let chat = reactive({});

		let template = reactive({
			basic: {}, // 基本信息
			home: {
				// 首页模板
				// style: {},
				// data: [],
			},
			user: {
				// 个人中心模板
				// style: {},
				// data: [],
			},
			setup: {},
			navigation: {},
		});

		let shareInfo = reactive({});

		// let has_wechat_trade_managed = ref(0);

		// 获取配置模板
		let init = async (templateId = null) => {
			console.log('init template');
			const networkStatus = await $platform.checkNetwork();
			if (!networkStatus) {
				$router.error('NetworkError');
			}

			// 检查配置
			if (typeof baseUrl === 'undefined') {
				$router.error('EnvError');
			}

			const res = await appApi.init(templateId, $platform.name);
			if (res.code === '0') {
				info.cdnurl = res.data.app.cdnUrl;
				info.about_us = res.data.app.about_us;
				info.privacy_protocol = res.data.app.privacy_protocol;
				info.copyright = res.data.app.copyright;
				info.copytime = res.data.app.copytime;
				info.logo = res.data.app.logo;
				info.version = res.data.app.version;
				info.name = res.data.app.name;
				info.user_protocol = res.data.app.user_protocol;
				// platform = res.data.platform;
				// username.value = 'hello world';
				// template = res.data.template;

				platform.auto_login = res.data.platform?.autoLogin || 0;
				platform.bind_mobile = res.data.platform?.bindMobile || 0;
				platform.share.methods = res.data.platform?.methods || [];
				platform.share.linkAddress = res.data.platform?.shareUrl || '';
				platform.share.forwardInfo.title = res.data.platform?.title || '';
				platform.share.forwardInfo.subtitle = res.data.platform?.subTitle || '';
				platform.share.forwardInfo.image = res.data.platform?.shareImg || '';
				platform.share.posterInfo.user_bg = res.data.platform?.userPoster || '';
				platform.share.posterInfo.goods_bg = res.data.platform?.goodsPoster || '';
				platform.share.posterInfo.groupon_bg = res.data.platform?.groupPoster || '';

				template.basic = res.data.template.basic;
				template.setup = res.data.template.setup;
				template.user = res.data.template.user;
				template.home = res.data.template.home;
				template.navigation = res.data.template.navigation;
				console.log('info: ', info);
				if (!res.data.template) {
					$router.error('TemplateError');
				}
				chat = res.data.chat;

				// 加载主题
				const sysStore = sys();

				// 设置主题
				sysStore.setTheme();
				sysStore.setMode();
				sysStore.setFontSize();
				sysStore.setBgImg();
				sysStore.setBgColor();
				// 模拟用户登录
				const userStore = user();
				console.log('userStore.isLogin: ', userStore.isLogin);

				// if (userStore.isLogin) {
				// 	console.log('需要登录');
				// 	userStore.loginAfter();
				// }

				return Promise.resolve(true);
			} else {
				$router.error('InitError', res.msg || '加载失败');
			}
		};

		return {
			info,
			platform,
			chat,
			template,
			shareInfo,
			init
		};
	}, {
		persist: {
			enabled: true, // 这个配置代表存储生效,而且是整个store都存储,默认情况下整个状态将存储在 sessionStorage 中
		},
	},
);

export default app;